<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/9/12
 * Time: 14:56
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Oftenproblem extends Base
{
    /**
     * 常见问题首页展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $params = input('param.');
            $where = [];
            // 端
            if(isset($params['is_end']) && $params['is_end'] != ''){
                $where['is_end'] =$params['is_end'] ;
            }
            // 描述
            if(isset($params['problem_describe']) && $params['problem_describe'] != ''){
                $where['problem_describe'] = ['like', '%' . $params['problem_describe'] . '%'];
            }
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_problem')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_problem')
                ->field('problem_id, problem_describe, problem_answer, is_end, create_time')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('create_time desc')
                ->select();
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    
    /**
     * 添加常见问题
     * @return mixed|\think\response\Json
     */
    public function problemAdd(){
        if(Request::instance()->isPost()){
            $problem_describe = input('problem_describe');
            $problem_answer = input('problem_answer');
            $is_end = input('is_end');
            
            $insert = [
                'problem_describe' => $problem_describe,
                'problem_answer' => $problem_answer,
                'is_end' => $is_end,
                'create_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_problem')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('oftenproblem/problemadd');
    }
    
    /**
     * 编辑
     * @return mixed|\think\response\Json
     */
    public function problemEdit(){
        if(Request::instance()->isPost()){
            $id = input('problem_id');
            $problem_describe = input('problem_describe');
            $problem_answer = input('problem_answer');
            
            $update = [
                'problem_describe' => $problem_describe,
                'problem_answer' => $problem_answer
            ];
            $res = Db::table('up_problem')->where('problem_id', $id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $problem_id = input('problem_id');
        $info = Db::table('up_problem')->where('problem_id', $problem_id)->find();
        $this->assign('info', $info);
        return $this->fetch('oftenproblem/problemedit');
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function problemDel(){
        $id = input('id');
        $res = Db::table('up_problem')->where('problem_id', $id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}